package org.linlinjava.litemall.mechanismadmin.web;

import com.github.pagehelper.PageInfo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallWalletRecord;
import org.linlinjava.litemall.db.service.LitemallWalletRecordService;
import org.linlinjava.litemall.mechanismadmin.annotation.LoginAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 钱包记录
 *
 * @author zhy
 * @create 2018-12-06 10:53
 **/
@RestController
@RequestMapping("/mechanismadmin/wallet")
public class MechanismWalletRecordController {
	private final Log logger = LogFactory.getLog(MechanismWalletRecordController.class);
	@Autowired
	private LitemallWalletRecordService litemallWalletRecordService;

	@GetMapping("/list")
	public Object getWallet(@LoginAdmin Integer adminId,
	    Byte value,String value2,
		Integer mechanismId,
	    @RequestParam(value = "page", defaultValue = "1") Integer page,
		@RequestParam(value = "limit", defaultValue = "10") Integer limit){
		if (adminId == null) {
			return ResponseUtil.unlogin();
		}
		LocalDateTime time = null;
		LocalDateTime time2 = null;
		if(!StringUtils.isEmpty(value2)){
			String startTime=value2+" 00:00:00";
			String endTime = value2+" 23:59:59";
			time = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
			time2 = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
		}

		List<LitemallWalletRecord> records = litemallWalletRecordService.querySelectiveByMerchaismId(adminId,time,time2,value,page,limit);
        long total = PageInfo.of(records).getTotal();
		Map<String, Object> data = new HashMap<>(16);
		data.put("total", total);
		data.put("items", records);

		return ResponseUtil.ok(data);
	}
}
